package com.zhuxiang.test4;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;

public class ServerThread extends Thread {
    private Socket s;
    public ServerThread(Socket s){
        this.s = s;
    }
    @Override
    public void run(){
        ObjectInputStream ois = null;
        ObjectOutputStream oos = null;
        try {
            ois = new ObjectInputStream(s.getInputStream());
            UserMessage tum = (UserMessage) ois.readObject();
            System.out.println(tum);
            if ("admin".equals(tum.getUser().getUserName()) && "123456".equals(tum.getUser().getPassword())){
                tum.setType("success");
            }else {
                tum.setType("fail");
            }
            oos = new ObjectOutputStream(s.getOutputStream());
            oos.writeObject(tum);
            oos.flush();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            if (null != oos){
                try {
                    oos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (null != ois){
                try {
                    ois.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (null != s){
                try {
                    s.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
